package gui;

import gui.AbstractViewPane.ViewMode;
import gui.nodes.AbstractNode;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import persistence.db.DataSet;

import business.EnseignantBL;
import business.FormationBL;

public class FormationUIConsulter extends AbstractViewPane {
	
	private JTextField fieldNom;
	private JTextArea areaDesc;

	private  int id = -1;
	private  String nom = ""; 
	private  String desc = "";
	private  DataSet anneeEtudeSet = null;
		
	private ViewMode currentViewMode;
	private AbstractNode creator;
	
	
	private ComponentList componentList = new ComponentList();
	
	private void showCommandBox() {
		JPanel commandBox = new JPanel();
		JButton save = new JButton("Enregistrer");
		save.setActionCommand("Enregistrer");
		save.addActionListener(new FormationListener());
		commandBox.add(save);
		drawCommandBox(commandBox);
	}
	
	public void initGUI(int idFormation) {

        if(currentViewMode != ViewMode.AJOUT){
        	FormationBL formationBL = new FormationBL(idFormation);
		    nom = formationBL.getNomFormation();
		    desc = formationBL.getDescriptionFormation();
		    anneeEtudeSet = formationBL.getAnneeEtudeSet();
        }
		
        //Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());     
        JLabel l = new JLabel("Nom : ", JLabel.TRAILING);
        p.add(l);

        fieldNom = new JTextField (nom);

        l.setLabelFor(fieldNom);
        p.add(fieldNom);
        componentList.add(fieldNom);
        
        l = new JLabel("Description", JLabel.TRAILING);
        p.add(l);
        areaDesc = new JTextArea (desc);
        areaDesc.setRows(5);
        l.setLabelFor(areaDesc);
        p.add(areaDesc);
        componentList.add(areaDesc);
        
        //Lay out the panel.
        SpringUtilities.makeCompactGrid(p,
                                        2, 2, //rows, cols
                                        6, 6,        //initX, initY
                                        6, 6);       //xPad, yPad
        
        add(p, BorderLayout.NORTH);
        if (currentViewMode != ViewMode.CONSULTATION) {
        	// Mode modification - afficher les boutons
        	showCommandBox();
        } else {
        	componentList.disableAll();
        }
        
	}
	
	public FormationUIConsulter(AbstractNode creator, ViewMode v, int idFormation) {
		this.creator = creator;
		currentViewMode = v;
		id = idFormation;
		initGUI(idFormation);
	}
	
	@Override
	public String getTitle() {
		// TODO Auto-generated method stub
		if (currentViewMode == ViewMode.CONSULTATION)
			return "Consultation d'une formation";
		else if (currentViewMode == ViewMode.AJOUT)
			return "Ajout d'une formation";
		else
			return "Modification d'une formation";
	}

	@Override
	public boolean hasBeenModified() {
		return (currentViewMode != ViewMode.CONSULTATION);
	}

	
	//inner class chargée de gérer l'écoute du bouton d'enregistrement du formulaire
	private class FormationListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			if(arg0.getActionCommand().equals("Enregistrer")){
				System.out.println("Demande d'enregistrement");
				FormationBL ensSave = new FormationBL();
				//si -1, c'est un ajout d'enseignant, sinon modification de l'enseignant de l'id concerné
				if(currentViewMode == ViewMode.AJOUT){
					ensSave.ajouterFormation(getID(), getNom(), getDesc(), getAnneeEtudeSet());
					componentList.disableAll();
				} else {
					ensSave.modifierFormation(getID(), getNom(), getDesc(), getAnneeEtudeSet());
					componentList.disableAll();
				}
				// Rafraichir le noeud de l'arbre
				creator.refresh();
				
				// Passer en mode consultation
				componentList.disableAll();
				currentViewMode = ViewMode.CONSULTATION;
				
			}	
		}
	}

	
	//accesseurs
	public int getID() {
		return id;
	}
	
	public String getNom() {
		return fieldNom.getText();
	}

	public String getDesc() {
		return areaDesc.getText();
	}

	public DataSet getAnneeEtudeSet(){
		return anneeEtudeSet;
	}
	
}
